package org.spring.springboot.controller;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.spring.springboot.domain.Category;
import org.spring.springboot.domain.JsonVO;
import org.spring.springboot.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/toolcategory")
public class CategoryController {

    @Autowired
    private ICategoryService svc;

    @PostMapping("/getAllAsTree")
    public List<Object> getAllAsTree(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String userid = "";
        if (jsonObject.has("userid")){
            userid = jsonObject.getString("userid");
        }
        else{
            return null;
        }

        List<Category> list= svc.selectAll(userid);
        System.out.println(list.size());

        JSONArray arr = new JSONArray();
        for (Category p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }
        return arr.toList();
    }


    @PostMapping("/delete")
    public JsonVO delete(@RequestBody Map<String, Object> jsonMap) {
    	JsonVO vo = null;
        JSONObject jsonObject = new JSONObject(jsonMap);
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        //判断是否存在下级节点
        List l = svc.selectByPid(id);
        if(l!=null && l.size()>0) {
        	vo = new JsonVO("error", "分类【"+name+"】存在下级节点，不允许删除");
        	return vo;
        }
        svc.delete(id);
        vo = new JsonVO("success", "");
        return vo;
    }

    @PostMapping("/add")
    public JsonVO add(@RequestBody Map<String, Object> jsonMap) {
    	JsonVO vo = null;
        JSONObject jsonObject = new JSONObject(jsonMap);
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        String title = jsonObject.getString("title");
        //判断重复
        List l = svc.selectByNameAndTitle(name, title);
        if(l!=null && l.size()>0) {
        	vo = new JsonVO("error", "分类名称【"+name+"】重复");
        	return vo;
        }
        String pid = jsonObject.getString("pid");
        String userid = jsonObject.getString("userid");
        Category category = new Category(id, pid, name, title, userid);
        svc.save(category);
        vo = new JsonVO("success", "");
        return vo;
    }
    
    @PostMapping("/update")
    public JsonVO update(@RequestBody Map<String, Object> jsonMap) {
    	JsonVO vo = null;
        JSONObject jsonObject = new JSONObject(jsonMap);
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        String title = jsonObject.getString("title");
      //判断重复
        List l = svc.selectByNameAndTitle(name, title);
        if(l!=null && l.size()>0) {
        	vo = new JsonVO("error", "分类名称【"+name+"】重复");
        	return vo;
        }
        //
        svc.update(id, name, title);
        vo = new JsonVO("success", "");
        return vo;
    }
}
